﻿Partial Public Class RibbonMain
    Private uc As UserControl
    Public Shared isDirty As Boolean = False
    Public Shared v_username As String

    Private Sub RibbonCommand_CanExecute(ByVal sender As System.Object, ByVal e As System.Windows.Input.CanExecuteRoutedEventArgs)
        e.CanExecute = True
    End Sub

    Private Sub ribbonTabSubLedger_Selected(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ribbonTabSubLedger.Selected
        GridViewContent.Children.Remove(uc)
        uc = New ucDepartmentLedger2
        GridViewContent.Children.Add(uc)
    End Sub

    Private Sub RibbonButtonDepartmentLedger_Click1(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonDepartmentLedger.Click
        GridViewContent.Children.Remove(uc)
        uc = New ucDepartmentLedger2
        GridViewContent.Children.Add(uc)
    End Sub

    Private Sub RibbonButtonLocationLedger_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonLocationLedger.Click
        GridViewContent.Children.Remove(uc)
        uc = New ucLocationLedger2
        GridViewContent.Children.Add(uc)
    End Sub

    Private Sub RibbonButtonCategoryLedger_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonCategoryLedger.Click
        GridViewContent.Children.Remove(uc)
        uc = New ucCategoryLedger2
        GridViewContent.Children.Add(uc)
    End Sub

    Private Sub RibbonButtonLeasedAssetLedger_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonLeasedAssetLedger.Click
        GridViewContent.Children.Remove(uc)
        uc = New ucLeasedAssetLedger2
        GridViewContent.Children.Add(uc)
    End Sub

    Private Sub ribbonTabDataForms_Selected(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ribbonTabDataForms.Selected
        Try
            GridViewContent.Children.Remove(uc)
            uc = New ucItems2
            GridViewContent.Children.Add(uc)
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub RibbonButtonCategories_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonCategories.Click
        GridViewContent.Children.Remove(uc)
        uc = New ucCategories2
        GridViewContent.Children.Add(uc)
    End Sub

    Private Sub RibbonButtonLocations_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonLocations.Click
        GridViewContent.Children.Remove(uc)
        uc = New ucLocations2
        GridViewContent.Children.Add(uc)
    End Sub

    Private Sub RibbonButtonPersons_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonPersons.Click
        GridViewContent.Children.Remove(uc)
        uc = New ucPersons2
        GridViewContent.Children.Add(uc)
    End Sub

    Private Sub RibbonButtonDepartments_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonDepartments.Click
        GridViewContent.Children.Remove(uc)
        uc = New ucDepartments2
        GridViewContent.Children.Add(uc)
    End Sub

    Private Sub RibbonButtonItems_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonItems.Click
        GridViewContent.Children.Remove(uc)
        uc = New ucItems2
        GridViewContent.Children.Add(uc)
    End Sub

    Private Sub RibbonButtonSynch_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonSynch.Click
        GridViewContent.Children.Remove(uc)
        uc = New ucSynchronise
        GridViewContent.Children.Add(uc)
    End Sub

    Private Sub ribbonTabSynch_Selected(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ribbonTabSynch.Selected
        GridViewContent.Children.Remove(uc)
        uc = New ucSynchronise
        GridViewContent.Children.Add(uc)
    End Sub

    Private Sub ribbonTabAdmin_Selected(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ribbonTabAdmin.Selected
        GridViewContent.Children.Remove(uc)
        uc = New ucUsers2
        GridViewContent.Children.Add(uc)
    End Sub

    Private Sub RibbonButtonUsers_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonUsers.Click
        GridViewContent.Children.Remove(uc)
        uc = New ucUsers2
        GridViewContent.Children.Add(uc)
    End Sub

    Private Sub RibbonButtonAccountPolicies_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)

        GridViewContent.Children.Remove(uc)
        uc = New ucAccountPolicies
        GridViewContent.Children.Add(uc)
    End Sub

    Private Sub RibbonButtonReaderSettings_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonReaderSettings.Click
        GridViewContent.Children.Remove(uc)
        uc = New ucReaderSettings
        GridViewContent.Children.Add(uc)
    End Sub




    Private Sub ribbonTabReports_Selected(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ribbonTabReports.Selected
        GridViewContent.Children.Remove(uc)
        uc = New ucReport
        GridViewContent.Children.Add(uc)
    End Sub

    Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        DisplayLoginScreen()

    End Sub

    Private Sub DisplayLoginScreen()
        Dim frm As New frmLogin()

        frm.Owner = Me
        frm.ShowDialog()
        If frm.DialogResult.HasValue And frm.DialogResult.Value Then
            'MessageBox.Show("User Logged In")
            labelUser.Content = frm.txtUserName.Text
        Else
            Application.Current.Shutdown()
        End If
    End Sub

    Private Sub DisplayChangePasswordScreen()
        Dim frm As New frmChangePassword()

        frm.Owner = Me
        frm.ShowDialog()
        If frm.DialogResult.HasValue And frm.DialogResult.Value Then
            '    'MessageBox.Show("User Logged In")
            '    labelUser.Content = frm.txtUserName.Text
            'Else
            '    Application.Current.Shutdown()
        End If
    End Sub

    Private Sub RibbonButton1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButton1.Click
        GridViewContent.Children.Remove(uc)
        uc = New ucSynchronise
        GridViewContent.Children.Add(uc)

        mainRibbon.SelectedTab = ribbonTabSynch
    End Sub

    Private Sub mainRibbon_PreviewMouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles mainRibbon.PreviewMouseLeftButtonDown
        If isDirty Then
            MsgBox("You have unsaved entries. Save or cancel changes before exiting this form.", MsgBoxStyle.OkOnly)
            e.Handled = False
        End If
    End Sub

    Private Sub RibbonTButtonAccountPolicies_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonTButtonAccountPolicies.Click
        GridViewContent.Children.Remove(uc)
        uc = New ucAccountPolicies
        GridViewContent.Children.Add(uc)
    End Sub

    Private Sub RibbonTButtonAccountPolicies_LostFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonTButtonAccountPolicies.LostFocus
        RibbonTButtonAccountPolicies.IsChecked = False

    End Sub


    Private Sub RibbonButton2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButton2.Click, RibbonApplicationMenuItemLogOff.Click
        DisplayLoginScreen()


    End Sub

    Private Sub RibbonApplicationMenuItemLogExit_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonApplicationMenuItemLogExit.Click

        Application.Current.Shutdown()

    End Sub

    Private Sub RibbonApplicationMenuItemChangePassword_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonApplicationMenuItemChangePassword.Click
        DisplayChangePasswordScreen()
    End Sub

    Private Sub tbBackupDatabase_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles tbBackupDatabase.MouseDown
        Dim dlg As New System.Windows.Forms.FolderBrowserDialog
        If dlg.ShowDialog() = Forms.DialogResult.OK Then
            Try
                If SQLClass.SQLBackupDatabase("localhost\sqlexpress", "ATS", dlg.SelectedPath & "\ATS" & Now.ToString("yyyyMMdd") & ".bak") Then
                    MessageBox.Show("ATS database backup successful : " & dlg.SelectedPath & "\ATS" & Now.ToString("yyyyMMdd") & ".bak")
                Else
                    Throw New Exception("Please check Event viewer for exact error.")

                End If

            Catch ex As Exception
                MessageBox.Show("Failed to create backup file. Error : " & ex.ToString)
            End Try

        End If
    End Sub

    Private Sub tbRestoreDatabase_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles tbRestoreDatabase.MouseDown
        Dim dlg As New Microsoft.Win32.OpenFileDialog()

        Dim result As Boolean = dlg.ShowDialog()

        ' Process open file dialog box results
        If result = True Then
            Try
                Dim messageBoxText As String = dlg.FileName & ControlChars.Lf & "Do you really want to restore from this backup file? This will overwrite the current data in the database."
                Dim caption As String = "Restore ATS database"
                Dim button As MessageBoxButton = MessageBoxButton.OKCancel
                Dim icon As MessageBoxImage = MessageBoxImage.Warning

                If MessageBox.Show(messageBoxText, caption, button, icon) = MessageBoxResult.OK Then
                    If SQLClass.SQLRestoreDatabase("localhost\sqlexpress", "ATS", dlg.FileName) Then
                        MessageBox.Show("ATS database restore successful : " & dlg.FileName)
                    Else
                        Throw New Exception("Please check Event viewer for error details.")

                    End If

                End If
            Catch ex As Exception
                MessageBox.Show("Failed to restore from backup file. Error : " & ex.ToString)
            End Try
        End If





    End Sub

    Private Sub RibbonButtonReport_Click1(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonReport.Click
        GridViewContent.Children.Remove(uc)
        uc = New ucReport
        GridViewContent.Children.Add(uc)
    End Sub


    Private Sub RibbonButtonConfigReport_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonConfigReport.Click
        GridViewContent.Children.Remove(uc)
        uc = New ucConfigReport
        GridViewContent.Children.Add(uc)
    End Sub
End Class


Public Class TreeViewLineConverter
    Implements IValueConverter
    Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Dim item As TreeViewItem = DirectCast(value, TreeViewItem)
        Dim ic As ItemsControl = ItemsControl.ItemsControlFromItemContainer(item)
        Return (ic.ItemContainerGenerator.IndexFromContainer(item) = ic.Items.Count - 1)
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Throw (New Exception("The method or operation is not implemented."))
    End Function
End Class

